package org.niu.str;

import java.util.Comparator;
import java.util.List;

public class FindLongestWord {
    boolean isContains(String s1, String s2){
        int i = 0,j = 0;
        while(i < s1.length() && j < s2.length()){
            if(s1.charAt(i) == s2.charAt(j)){
                i++;
                j++;
            }else i++;
        }
        if(j == s2.length()-1){
            return true;
        }
        return false;
    }
    public String findLongestWord(String s, List<String> dictionary) {
        dictionary.sort(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                if(o1.length()!=o2.length()){
                    return o2.length()-o1.length();
                }
                return o1.compareTo(o2);
            }
        });
        for(String dic:dictionary){
            if(isContains(s,dic)){
                return dic;
            }
        }
        return "";
    }

}
